#!/usr/bin/make -f

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# only build cockpit-pcp if pcp is available
ifneq ($(shell dpkg -s libpcp3-dev >/dev/null 2>&1 && echo yes),yes)
	export DH_OPTIONS = -Ncockpit-pcp
	CONFIG_OPTIONS = --disable-pcp
endif

# newer firewalld ships cockpit.xml, but keep it for backports
FIREWALLD_SERVICE = $(findstring $(shell . /etc/os-release; echo $$VERSION_ID),9 18.04)
ifeq ($(FIREWALLD_SERVICE),)
	WS_CONFLICTS = firewalld (<< 0.6.0)
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		--with-networkmanager-needs-root=yes \
		--with-cockpit-user=cockpit-ws \
		--with-cockpit-ws-instance-user=cockpit-wsinstance \
		--with-appstream-config-packages='[ "appstream" ]' \
		--with-nfs-client-packages='"nfs-common"' \
		--with-pamdir=/lib/$(DEB_HOST_MULTIARCH)/security \
		--libexecdir=/usr/lib/cockpit $(CONFIG_OPTIONS)

override_dh_systemd_enable:
	dh_systemd_enable -p cockpit-ws --name=cockpit cockpit.socket

override_dh_systemd_start:
	dh_systemd_start -p cockpit-ws cockpit.socket

override_dh_install:
	# Debian/Ubuntu PAM config
	mkdir -p debian/tmp/etc/pam.d
	install -p -m 644 tools/cockpit.debian.pam debian/tmp/etc/pam.d/cockpit

	# don't ship broken branding symlinks
	for d in rhel fedora centos scientific; do rm -r debian/tmp/usr/share/cockpit/branding/$$d; done
	dpkg-vendor --derives-from ubuntu || rm -r debian/tmp/usr/share/cockpit/branding/ubuntu

	# unpackaged modules
	for m in kdump selinux; do rm -r debian/tmp/usr/share/cockpit/$$m; done
	rm debian/tmp/usr/share/metainfo/org.cockpit-project.cockpit-kdump.metainfo.xml
	rm debian/tmp/usr/share/metainfo/org.cockpit-project.cockpit-selinux.metainfo.xml

	dh_install --fail-missing -Xusr/src/debug
	make install-tests DESTDIR=debian/cockpit-tests

	if [ -z "$(FIREWALLD_SERVICE)" ] && [ -d debian/cockpit-ws ]; then rm debian/cockpit-ws/usr/lib/firewalld/services/cockpit.xml; rmdir -p --ignore-fail-on-non-empty debian/cockpit-ws/usr/lib/firewalld/services/; fi

override_dh_gencontrol:
	dh_gencontrol -- -Vbridge:minversion="$(shell tools/min-base-version)" -Vws:Conflicts="$(WS_CONFLICTS)"
